.SUFFIXES: .c .lo

COMPILE = $(CC) $(CFLAGS) -fPIC
INC_PATH = -Iinclude -I/usr/local/include
LIB_PATH = $(LIBS) -lfastcommon 
TARGET_LIB = $(TARGET_PREFIX)/$(LIB_VERSION)

TOP_HEADERS = sf_types.h sf_global.h sf_define.h sf_nio.h sf_service.h \
              sf_func.h sf_util.h sf_configs.h sf_proto.h sf_cluster_cfg.h \
              sf_sharding_htable.h sf_connection_manager.h sf_serializer.h \
              sf_binlog_index.h sf_file_writer.h sf_binlog_writer.h \
              sf_ordered_writer.h sf_buffered_writer.h sf_iov.h \
              sf_shared_mbuffer.h

IDEMP_COMMON_HEADER = idempotency/common/idempotency_types.h

IDEMP_SERVER_HEADER = idempotency/server/server_types.h \
                      idempotency/server/server_channel.h  \
                      idempotency/server/request_htable.h  \
                      idempotency/server/channel_htable.h  \
                      idempotency/server/server_handler.h  \
                      idempotency/server/request_metadata.h

IDEMP_CLIENT_HEADER = idempotency/client/client_types.h    \
                      idempotency/client/receipt_handler.h \
                      idempotency/client/client_channel.h  \
                      idempotency/client/rpc_wrapper.h

ALL_HEADERS  = $(TOP_HEADERS) $(IDEMP_SERVER_HEADER) $(IDEMP_CLIENT_HEADER)

SHARED_OBJS = sf_nio.lo sf_iov.lo sf_service.lo sf_global.lo \
        sf_func.lo sf_util.lo sf_configs.lo sf_proto.lo \
        sf_sharding_htable.lo sf_cluster_cfg.lo   \
        sf_connection_manager.lo sf_serializer.lo \
        sf_binlog_index.lo sf_file_writer.lo  \
        sf_binlog_writer.lo sf_ordered_writer.lo \
        sf_shared_mbuffer.lo \
        idempotency/server/server_channel.lo   \
        idempotency/server/request_htable.lo   \
        idempotency/server/channel_htable.lo   \
        idempotency/server/server_handler.lo   \
        idempotency/server/request_metadata.lo \
        idempotency/client/receipt_handler.lo  \
        idempotency/client/client_channel.lo

ALL_OBJS = $(SHARED_OBJS)
ALL_LIBS = libserverframe.so

all: $(ALL_LIBS)

libserverframe.so: $(SHARED_OBJS)

	cc -shared -o $@ $^ $(LIB_PATH)

.lo:
	$(COMPILE) -o $@ $<  $(SHARED_OBJS) $(LIB_PATH) $(INC_PATH)
.c:
	$(COMPILE) -o $@ $<  $(ALL_OBJS) $(LIB_PATH) $(INC_PATH)
.c.lo:
	$(COMPILE) -c -o $@ $<  $(INC_PATH)
install:
	mkdir -p $(TARGET_LIB)
	mkdir -p $(TARGET_PREFIX)/lib
	mkdir -p $(TARGET_PREFIX)/include/sf/idempotency/common
	mkdir -p $(TARGET_PREFIX)/include/sf/idempotency/server
	mkdir -p $(TARGET_PREFIX)/include/sf/idempotency/client

	install -m 755 $(ALL_LIBS) $(TARGET_LIB)
	cp -f $(TOP_HEADERS) $(TARGET_PREFIX)/include/sf
	cp -f $(IDEMP_COMMON_HEADER) $(TARGET_PREFIX)/include/sf/idempotency/common
	cp -f $(IDEMP_SERVER_HEADER) $(TARGET_PREFIX)/include/sf/idempotency/server
	cp -f $(IDEMP_CLIENT_HEADER) $(TARGET_PREFIX)/include/sf/idempotency/client

	@BUILDROOT=$$(echo "$(TARGET_PREFIX)" | grep BUILDROOT); \
	if [ -z "$$BUILDROOT" ] && [ ! -e $(TARGET_PREFIX)/lib/libserverframe.so ]; then ln -s $(TARGET_LIB)/libserverframe.so $(TARGET_PREFIX)/lib/libserverframe.so; fi
clean:
	rm -f $(ALL_OBJS) $(ALL_LIBS) $(ALL_PRGS)

